package com.inspector.app.ui.chat.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.inspector.app.BaseApp;
import com.inspector.app.R;
import com.inspector.app.databinding.ItemExecuteAdapterBinding;
import com.inspector.app.ui.chat.entity.UserEntity;

import java.util.ArrayList;
import java.util.List;

public class ExecuteAdapter extends RecyclerView.Adapter<ExecuteAdapter.ViewHolder> {

    private ArrayList<UserEntity> mList = new ArrayList<>();

    public void setData(List<UserEntity> dataList) {
        this.mList.clear();
        this.mList.addAll(dataList);
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public ExecuteAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemExecuteAdapterBinding inflate = ItemExecuteAdapterBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ExecuteAdapter.ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ExecuteAdapter.ViewHolder holder, int position) {
        UserEntity userEntity = mList.get(position);

        holder.binding.executePendingBindings();

        holder.bind(userEntity, position);
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ItemExecuteAdapterBinding binding;

        public ViewHolder(@NonNull ItemExecuteAdapterBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public void bind(UserEntity userEntity, int position) {
            if (userEntity != null) {
                String role = "";
                int source = userEntity.getSource();
                switch (source) {
                    case 1:
                        role = "指挥员";
                        break;
                    case 2:
                        role = "飞行员";
                        break;
                    case 3:
                        role = "观察员";
                        break;
                    default:
                        role = "未知角色";
                        break;
                }

                String realName = userEntity.getRealName();
                if (realName != null) {
                    String[] parts = realName.split("-", 2);
                    if (parts.length >= 2) {
                        String name = parts[0];
                        String phone = parts[1];

                        binding.name.setText(name + "(" + role + ")");
                        binding.phoneText.setText(phone);
                    } else {
                        // 如果realName的格式不符合预期，这里可以设置默认值或抛出异常
                        binding.name.setText(realName + "(" + role + ")");
                        binding.phoneText.setText("电话号码未提供");
                    }
                } else {
                    // 如果realName为null，这里可以设置默认值或抛出异常
                    binding.name.setText(role);
                    binding.phoneText.setText("电话号码未提供");
                }

                String headUrl = userEntity.getHeadUrl();
                if (headUrl != null) {
                    Glide.with(BaseApp.getInstance())
                            .load(headUrl)
                            .apply(RequestOptions.circleCropTransform())
                            .into(binding.chatItemHeader);
                } else {
                    // 如果头像URL为null，可以加载默认头像
                    Glide.with(BaseApp.getInstance())
                            .load(R.mipmap.head_default)
                            .apply(RequestOptions.circleCropTransform())
                            .into(binding.chatItemHeader);
                }
            }
        }
    }
}
